/**
 * Created by 蔡岳衡 on 2016/8/1.
 */

var operRecord = function(){

    var url = path + '/action/powerdb/designer/operRecord/';

    //操作记录查找表单选择器
    var SearchFromSeletor = '#operRecordSearchForm';

    var operRecordJqTableSeletor =  '#operRecordJqgridTable';

    var operRecordJqPagerSeletor =  '#operRecordJqgridPager';

    /** 枚举关键字定义 */
    var enums = ['pdm_oper_record-type','pdm_oper_record-jobStatus'];

    /** 获取所有需要翻译的枚举 */
    var enumMap = null;

    var resData;

    return {
        /*
         * 初始化表格
         */
        init: function () {

            function initEnum() {
                enumMap = sPost.getEnumMap(enums);
                sUtils.select2Enum({
                    selector: '#operRecordType',
                    enums: enumMap.get('pdm_oper_record-type'),
                    addAll: true
                });
            }

            initEnum();
            operRecord.search(true);
            operRecord.search(false);
        },

        search:function(flag){
            if (flag) { //第一次加載

                var recordGridSetting = {
                    gridId: operRecordJqTableSeletor,
                    pagerId: operRecordJqPagerSeletor,
                    // datatype: 'json',
                    datatype: 'local',
                    data: [],
                    //height: 200,
                    gridHeightType: 1,
                    url: null,
                    mtype: "post",
                    multiselect: true,
                    rownumbers: true,
                    autowidth: true,
                    rowNum: 30,
                    rowList: [15, 30, 50],
                    colNames: ['id', '操作人', '创建时间', '操作类型', '操作记录', '操作结果', '操作sql', '项目编号', '模块编号'],
                    colModel: [
                        {name: 'id', index: 'id', width: 0, align: 'center', hidden: true},
                        {name: 'accountId', index: 'accountId', width: 0, align: 'center'},
                        {name: 'createTime', index: 'createTime', width: 0, align: 'center'},
                        {name: 'type', index: 'type', width: 0, align: 'center'},
                        {name: 'record', index: 'record', width: 0, align: 'center'},
                        {name: 'jobStatus', index: 'jobStatus', width: 0, align: 'center'},
                        {name: 'operationSql', index: 'operationSql', width: 0, align: 'center'},
                        {name: 'project_id', index: 'project_id', width: 0, align: 'center'},
                        {name: 'module_id', index: 'module_id', width: 0, align: 'center'}
                    ]
                };

                slog.log("search one");
                sGrid.jqgridSimple(recordGridSetting);
                sGrid.jqgridReSize(recordGridSetting);
                sGrid.jqgridNotButton(recordGridSetting['gridId'], recordGridSetting['pagerId']);
                sGrid.jqgridDestory(recordGridSetting['gridId']);

            } else { // 第二次加載
                /*
                 * 获取查询表单中的参数
                 * 请求action,获取返回数据
                 */
                slog.log("search two");

                var params = sUtils.getFormParams(SearchFromSeletor);
                //$.extend(true,params,{'Q_createTime_>=_Date':'2016-08-03'})
                slog.log(params);

                sPost.ajaxFind(url, params, function (data) {
                    if (data.success) {
                        slog.log("Find Method 成功！");
                        resData = data.obj;

                        sUtils.transactionEnum(resData, enums, enumMap);
                        slog.log(resData);

                        $(operRecordJqTableSeletor).clearGridData();
                        if (resData != null) {
                            $(operRecordJqTableSeletor).setGridParam({data: resData});
                        }
                        $(operRecordJqTableSeletor).trigger("reloadGrid");
                    } else {
                        slog.error("find Method Error : " + data.msg);
                    }
                });
            }
        },
        /*
         * 清空查询条件
         */
        clear: function () {
            slog.log("清空");
            sUtils.cleanFrom(SearchFromSeletor);
            $('#operRecordType').select2('val', '');
            operRecord.search(false);
        },
    }
}();